﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {
    $('#list').datagrid({
        title: '车辆管理',
        idField: 'CarID',
        fitColumns: 'true',
        autoRowHeight: true,
        singleSelect: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'CarInfo/GetList',
        columns: [[
			{ field: 'ck', checkbox: true },
			{ field: 'CarID', title: '车辆ID', hidden: true },
            { field: 'CarNumber', title: '车牌号码', width: 180 },
            { field: 'CarKind', title: '车辆类型', width: 180 },
			{ field: 'CarBrand', title: '车辆品牌', width: 180},
			{ field: 'CarPhote', title: '车辆照片', width: 180, hidden: true },
			{ field: 'Price', title: '购买价格', width: 180, hidden: true },
			{ field: 'PurchaseDateTime', title: '购买日期', width: 180, hidden: true },
			{ field: 'EngineCode', title: '发动机编号', width: 180, hidden: true },
			{ field: 'Displacement', title: '排气量', width: 180},
			{ field: 'Capacity', title: '容纳人数', width: 180},
			{
			    field: 'Status', title: '状态', width: 180,
			    formatter: function (value, row, index) {
			        if (value == 0) {
			            return "可用";
			        }
			        if (value == 1) {
			            return "损坏";
			        }
			        if (value == 2) {
			            return "维修  ";
			        }
			        if (value == 3) {
			            return "报废";
			        }
			    }
			},
			{ field: 'Remarks', title: '备注', width: 180, hidden: true },
		
        ]],
    });
}

//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    $('#Status').combobox('setValue', 0);//设置状态默认值为可用
    url = '/CarInfo/Create';
}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#fm').form('load', row);
        var date = formatDate(row.PurchaseDateTime, "yyyy-MM-dd ");
        $('img#user_head_180').imgAreaSelect({ remove: true });
        var path = "../UploadFiles/Images/180/" + row.CarPhote;
        $("#user_head_180").attr({
            "src": path
        });
        $("#PurchaseDateTime").datebox("setValue", date);
        url = '/CarInfo/Edit';
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/CarInfo/Delete', { id: rows.CarID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}